<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */

    /**
     * nc_old_age_buckle
     * 
     *  */
    public function nc_old_age_buckle()
    {
        $brand_colour = [
            'ncold age buckle_purple', 'ncold age buckle_pink', 'ncold age buckle_apricot',
        ];
        $cup_sz = [34, 36, 38, 40, 42, 44, 46, 48, 50];
        $cup_size = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

        foreach ($cup_sz as  $v) {
            foreach ($cup_size as  $vl) {
                $cup[] = $v . $vl;
            }
        }

        foreach ($brand_colour as  $v) {
            $sku_arr[$v] = 0;
            foreach ($cup as  $vl) {
                $sku_arr[$v . '_' . $vl] = 0;
            }
        }

        $sku_arr1['ncold age buckle'] = 0;
        $merge_sku = array_merge($sku_arr, $sku_arr1);
        $statistics1 = [];
        foreach ($statistics1 as $v) {
            $multiple = 1;
            if (strstr(strtolower($v['sku']), 'nc')) {
                $sku_lower = strtolower($v['sku']);
                $sku_title_arr =  explode('-',  str_replace('nc', 'nc-', $sku_lower));
                if (isset($sku_title_arr[0])) {
                    if (isset($sku_title_arr[1])) {
                        if (isset($sku_title_arr[3])) {
                            $multiple = $sku_title_arr[3];
                        }
                        $sku_title_arr1 =  explode('+', $sku_title_arr[1]);
                        foreach ($sku_title_arr1 as $vl) {
                            $merge_sku[$sku_title_arr[0]] = $merge_sku[$sku_title_arr[0]] + $v['count'] * $multiple;
                            $merge_sku[$sku_title_arr[0] . '_' . $vl] = $merge_sku[$sku_title_arr[0] . '_' . $vl] + $v['count'] * $multiple;
                            if (isset($sku_title_arr[2])) {
                                $merge_sku[$sku_title_arr[0] . '_' . $vl . '_' . $sku_title_arr[2]] = $merge_sku[$sku_title_arr[0] . '_' . $vl . '_' . $sku_title_arr[2]]  + $v['count'] * $multiple;
                            }
                        }
                    }
                }
            }
        }


        dump($merge_sku);
        die;
    }


    public function index()
    {


        $band = ['a', 'b', 'c'];
        $color = ['red', 'green', 'black'];
        $size = ['l', 'xl', 'xxl'];
        $cup = ['36a', '36b', '36c'];
        $arr = [
            'brand' => $band,
            'color' => $color,
            'size' => $size,
            'cup' => $cup
        ];
        return json($arr);

        $brand_colour = [
            'bra06_black', 'bra06_nude'
        ];
        $cup_size = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];

        foreach ($brand_colour as  $v) {
            $sku_size_arr[$v] = 0;
            foreach ($cup_size as  $vl) {
                $sku_size_arr[$v . '_' . $vl] = 0;
            }
        }
        $sku_arr_zdy['bra06'] = 0;
        $merge_sku = array_merge($sku_size_arr, $sku_arr_zdy);
        dump($merge_sku);
        die;
        die;
    }

    //法国增值税号生成器接口
    public function French_VAT_number_generator()
    {
        $siren = mt_rand(100000000, 999999999);
        $key = (12 + 3 * ($siren % 97)) % 97;
        $return =  'FR' . $key . $siren;
        return $return;
    }

    /**
     * 接口描述：数据导出  index/index/outExcel
     * @author 特伦不纯、
     * @date 2022-10-11 
     */
    public function outExcel()
    {
        // dump(import('fast', EXTEND_PATH));
        // die;
        //1.清除缓冲区，避免乱码
        //2.导入PHPExcel类库
        //3.查询出数据库数据
        //4.实例化PHPExcel对象
        //5.设置sheet的名称,以及单元格字段信息
        //6.设置内容
        //7.通过PHPExcel_IOFactory的写函数将上面数据写出来
        //8.设置表格文件的名称
        //9.生成并下载表格
        //====================================================

        //1.清除缓冲区,避免乱码
        ob_end_clean();
        //2.导入PHPExcel类库
        vendor("PHPExcel.PHPExcel");
        vendor("PHPExcel.PHPExcel.IOFactory");
        vendor("PHPExcel.PHPExcel.Writer.Excel2007");

        //3.查询出数据库数据
        $data =  [
            ['Id' => 1, 'name' => 13260619207, 'headimg' => 1],
            ['Id' => 2, 'name' => 13260619207, 'headimg' => 1],
            ['Id' => 3, 'name' => 13260619207, 'headimg' => 1],
            ['Id' => 4, 'name' => 13260619207, 'headimg' => 1],
            ['Id' => 5, 'name' => 13260619207, 'headimg' => 1],
            ['Id' => 6, 'name' => 13260619207, 'headimg' => 1],
        ];

        //4.实例化PHPExcel对象
        /**  
         * 实例化之后的PHPExcel对象类似于一个暂存于内存中文档文件，  
         * 可以对它进行操作以达到修改文档数据的目的 
         */
        $objPHPExcel = new \PHPExcel(); //通过new关键字创建空白文档  

        $PHPWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel); //用于2007版本格式

        //5.设置sheet的名称,以及单元格字段信息
        $objPHPExcel->getActiveSheet()->setTitle('test'); //获取当前的工作表并且设置工作表名称
        $objPHPExcel->setActiveSheetIndex(0) //设置sheet（工作表）的起始位置为0
            ->setCellValue('A1', 'ID编号')  //设置单元格字段信息
            ->setCellValue('B1', '商品名称')
            ->setCellValue('C1', '商品价钱');

        //6.设置内容
        $count = count($data);  //计算有多少条数据
        //设置内容 i=2表示从第二段开始设值
        for ($i = 2; $i <= $count + 1; $i++) {
            //填入数据详情是这样的 A2,data[0]['Id'];
            //                   A3,data[1]['id'];      以此类推
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $data[$i - 2]['Id']);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $data[$i - 2]['name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $data[$i - 2]['headimg']);
        }

        //7.通过PHPExcel_IOFactory的写函数将上面数据写出来
        $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");

        //8.设置表格文件的名称
        $outputFileName = 'total.xls';

        header("Content-Type: application/force-download");

        header("Content-Type: application/octet-stream");

        header("Content-Type: application/download");

        header('Content-Disposition:inline;filename="' . $outputFileName . '"');

        header("Content-Transfer-Encoding: binary");

        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

        header("Pragma: no-cache");
        //9.生成并下载表格
        $excel_result = $PHPWriter->save("php://output");
        if ($excel_result) {
            $this->success('导出成功');
        } else {
            $this->success('导出失败');
        }
    }
}
